package com.example.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.enums.ShortLinkStateEnum;
import com.example.manager.GroupCodeMappingManager;
import com.example.mapper.GroupCodeMappingMapper;
import com.example.model.GroupCodeMappingDO;
import com.example.vo.GroupCodeMappingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/6/916:41
 */
@Component
@Slf4j
public class GroupCodeMappingManagerImpl implements GroupCodeMappingManager {

    @Autowired
    GroupCodeMappingMapper groupCodeMappingMapper;

    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long accountNo, Long groupId) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>().eq("id", mappingId)
                .eq("account_no", accountNo).
                eq("group_id", groupId));
    }

    @Override
    public int add(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.insert(groupCodeMappingDO);
    }

    @Override
    public int del(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id", groupCodeMappingDO.getId())
                .eq("group_id", groupCodeMappingDO.getGroupId())//防止商家越组删数据
                .eq("account_no",groupCodeMappingDO.getAccountNo())//为了阻止其他商家去删除这个数据
                .set("del", 1));
    }

    @Override
    public Map<String, Object> pageShortLinkByGroupId(Integer page, Integer size, Long accountNo, Long groupId) {
        Page<GroupCodeMappingDO> pageInfo = new Page<>(page,size,true);

        Page<GroupCodeMappingDO> groupCodeMappingDOPage = groupCodeMappingMapper.selectPage(pageInfo, new QueryWrapper<GroupCodeMappingDO>()
                .eq("account_no", accountNo)
                .eq("group_id", groupId));
        HashMap<String, Object> pageMap = new HashMap<>();
        pageMap.put("total_record",groupCodeMappingDOPage.getTotal());
        pageMap.put("total_page",groupCodeMappingDOPage.getPages());
        pageMap.put("current_data",groupCodeMappingDOPage.getRecords()
                .stream()
                .map(this::beanProcess)
                .collect(Collectors.toList()));
        return pageMap;
    }

    /**
     * 深拷贝GroupCodeMappingVO类的方法
     * @param obj
     * @return
     */
    private GroupCodeMappingVO beanProcess(GroupCodeMappingDO obj) {
        GroupCodeMappingVO targetObj = new GroupCodeMappingVO();
        BeanUtils.copyProperties(obj,targetObj);
        return targetObj;
    }

    @Override
    public int updateGroupCodeMappingStatus(Long accountNo, Long groupId, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {
        int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("code", shortLinkCode)
                .eq("account_no", accountNo)
                .eq("group_id", groupId)
                .eq("del",0)
                .set("state", shortLinkStateEnum.name()));
        return rows;
    }

    @Override
    public GroupCodeMappingDO findByCodeAndGroupId(String shortLinkCode, Long groupId, Long accountNo) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("code", shortLinkCode)
                .eq("group_id", groupId)
                .eq("account_no", accountNo));
    }

    @Override
    public int update(GroupCodeMappingDO groupCodeMappingDO) {
        int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id", groupCodeMappingDO.getId())
                .eq("account_no", groupCodeMappingDO.getAccountNo())
                .eq("group_id", groupCodeMappingDO.getGroupId())
                .eq("del", 0)
                .set("title", groupCodeMappingDO.getTitle())
                .set("domain", groupCodeMappingDO.getDomain()));
        return rows;
    }
}
